package com.openspring.platform.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import com.openspring.framework.dao.AbstractSpringGenericDAO;
import com.openspring.framework.extend.spring.EntityPropertyRowMapper;
import com.openspring.platform.dao.ResourceDAO;
import com.openspring.platform.model.Resource;

@Repository("resourceDAO")
public class ResourceDAOImpl extends AbstractSpringGenericDAO<Resource, Long> implements ResourceDAO {

	@Override
	public List<Resource> queryAllChildren(Long id) {
		String sql = "WITH RECURSIVE r AS (" +
				"SELECT * FROM emp.t_platform_resource WHERE id = ? UNION ALL " +
				"SELECT t.* FROM emp.t_platform_resource t, r WHERE t.\"parentId\" = r.id )" +
				"SELECT * FROM r ORDER BY r.sort";
		return getJdbcTemplate().query(sql, new EntityPropertyRowMapper<Resource>(Resource.class), id);
	}

	@Override
	public List<Resource> queryChildren(Long id) {
		String sql = "SELECT * FROM emp.t_platform_resource WHERE \"parentId\" = ? ORDER BY sort";
		return getJdbcTemplate().query(sql, new EntityPropertyRowMapper<Resource>(Resource.class), id);
	}

	@Override
	public List<Resource> queryEnableChildren(Long id) {
		String sql = "SELECT * FROM emp.t_platform_resource WHERE \"parentId\" = ? AND status = ? ORDER BY sort";
		return getJdbcTemplate().query(sql, new EntityPropertyRowMapper<Resource>(Resource.class), id, "enable");
	}

}
